package cn.lzm.org.service.impl;

import cn.lzm.basic.config.BussinessException;
import cn.lzm.basic.query.BaseQuery;
import cn.lzm.basic.util.BaiduAuditUtils;
import cn.lzm.basic.util.PageList;
import cn.lzm.org.domain.Employee;
import cn.lzm.org.domain.Shop;
import cn.lzm.org.domain.ShopAuditLog;
import cn.lzm.org.mapper.EmployeeMapper;
import cn.lzm.org.mapper.ShopAuditLogMapper;
import cn.lzm.org.mapper.ShopMapper;
import cn.lzm.org.service.IShopService;
import cn.lzm.basic.service.impl.BaseServiceImpl;
import cn.lzm.org.vo.ShopVo;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzm
 * @since 2022-10-15
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    // 注入邮件对象
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        // 校验名字不能为空
        if (StringUtils.isBlank(shop.getName())) {
            throw new BussinessException("店铺名字不能为空！！");
        }
        if (StringUtils.isBlank(shop.getTel())) {
            throw new BussinessException("店铺电话不能为空");
        }
        if (StringUtils.isBlank(shop.getAdmin().getUsername())) {
            throw new BussinessException("管理员账号不能为空");
        }
        if (StringUtils.isBlank(shop.getAdmin().getPhone())) {
            throw new BussinessException("管理员手机不能为空");
        }
        if (StringUtils.isBlank(shop.getAdmin().getEmail())) {
            throw new BussinessException("管理员邮箱不能为空");
        }
        if (!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())) {
            System.out.println(shop.getAdmin().getPassword());
            System.out.println(shop.getAdmin().getComfirmPassword());
            throw new BussinessException("密码不一致");
        }
        // 判断点不明是否存在
        if (null != shopMapper.findByName(shop.getName())) {
            throw new BussinessException("店铺名字重复");
        }
        // 使用百度AI判断名字是否非法和图片是否非法
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BussinessException("店铺名字非法");
        }
        if (BaiduAuditUtils.ImgCensor(shop.getLogo())) {
            throw new BussinessException("店铺Logo非法");
        }
        Employee em = employeeMapper.findByName(shop.getAdmin().getUsername());
        if (null != em && null != em.getShopId()) {
            Shop byId = shopMapper.findById(em.getId());
            if (Objects.equals(em.getShopId(), byId.getId()))
                throw new BussinessException("该管理员已注册店铺");
        }

        shopMapper.add(shop);
        if (null == em) {
            em = shop.getAdmin();

            employeeMapper.add(em);
        }
        em.setShopId(shop.getId());
        employeeMapper.update(em);

        // 修改shop表的adminId
        Shop uptShop = new Shop();
        uptShop.setId(shop.getId());
        uptShop.setAdminId(em.getId());
        shopMapper.update(uptShop);
    }

    @Override
    public void auditPass(ShopAuditLog shopAuditLog) {
        if (null != shopAuditLog) {
            shopAuditLog.setAuditId(1L);
            shopAuditLogMapper.add(shopAuditLog);
            // 1.准备发送的相关信息
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            // 1.1.设置发送人
            simpleMailMessage.setFrom("834291384@qq.com");
            // 1.2.设置接收人
            simpleMailMessage.setTo("2561232830@qq.com");
            // 1.3.设置邮件主题
            simpleMailMessage.setSubject("宠物之家店铺激活");
            // 1.4.设置邮件文本内容
            simpleMailMessage.setText("你的店铺已通过审核，请点击链接进行激活," + "激活连接：http://localhost:8080/shop/audit/" + shopAuditLog.getShopId());
            // 使用邮件核心对象发送邮件
            javaMailSender.send(simpleMailMessage);

            Long shopId = shopAuditLog.getShopId();
            Shop shop = new Shop();
            shop.setId(shopId);
            shop.setState(1);
            shopMapper.update(shop);


        }


    }

    @Override
    public void activation(Long shopId) {
        Shop shop = new Shop();
        shop.setId(shopId);
        shop.setState(2);
        shopMapper.update(shop);
    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) {
        if (shopAuditLog != null) {
            shopAuditLog.setAuditId(1L);
            shopAuditLogMapper.add(shopAuditLog);
            // 1.准备发送的相关信息
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            // 1.1.设置发送人
            simpleMailMessage.setFrom("834291384@qq.com");
            // 1.2.设置接收人
            simpleMailMessage.setTo("2561232830@qq.com");
            // 1.3.设置邮件主题
            simpleMailMessage.setSubject("宠物之家店铺驳回通知");
            // 1.4.设置邮件文本内容
            simpleMailMessage.setText("你的店铺未通过审核，因为" + shopAuditLog.getNote() + "请点击链接修改店铺信息：" + "http://localhost:8081/#/shopRegisterAgain?shopId=" + shopAuditLog.getShopId());
            // 使用邮件核心对象发送邮件
            javaMailSender.send(simpleMailMessage);

            Long shopId = shopAuditLog.getShopId();
            Shop shop = new Shop();
            shop.setId(shopId);
            shop.setState(-1);
            shopMapper.update(shop);
        }
    }

    @Override
    public void relocate(Shop shop) {
        if (StringUtils.isBlank(shop.getName())) {
            throw new BussinessException("店铺名字不能为空！！");
        }
        if (StringUtils.isBlank(shop.getTel())) {
            throw new BussinessException("店铺电话不能为空");
        }
        if (StringUtils.isBlank(shop.getAddress())) {
            throw new BussinessException("店铺地址不能为空！！");
        }
//        // 判断点不明是否存在
//        if (null != shopMapper.findByName(shop.getName())){
//            throw new BussinessException("店铺名字重复");
//        }
        // 使用百度AI判断名字是否非法和图片是否非法
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BussinessException("店铺名字非法");
        }
//        if (BaiduAuditUtils.ImgCensor(shop.getLogo())){
//            throw new BussinessException("店铺Logo非法");
//        }

        shop.setState(0);
        shopMapper.update(shop);
    }

    @Override
    public void batchAdd(List<Shop> shops) {
        shopMapper.batchAdd(shops);
    }

    @Override
    public Map<String, Object> loadShopEchartsData() {
        List<ShopVo> shopVos = shopMapper.loadShopEchartsData();
        List<Integer> x = shopVos.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> y = shopVos.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        Map<String, Object> data = new HashMap<>();
        data.put("x", x);
        data.put("y", y);
        return data;
    }

    @Override
    public PageList<Shop> queryPage(BaseQuery query) {
        if (!com.mysql.cj.util.StringUtils.isNullOrEmpty(query.getKeyword())) {
            query.setKeyword(query.getKeyword().trim());
        }
        Integer totals = shopMapper.queryByCount(query);
        if (null == totals || 0 == totals) {
            return new PageList<>(0, new ArrayList<>());
        }
        // 查询数据
        List<Shop> shopList = new ArrayList<>();
        List<Shop> shops = shopMapper.queryByPage(query);
        for (Shop shop : shops) {
            List<ShopAuditLog> auditLogList = new ArrayList<>();
            List<ShopAuditLog> all = shopAuditLogMapper.findAll();
            all.forEach(System.out::println);
            for (ShopAuditLog shopAuditLog : all) {
                if (shop.getId() == shopAuditLog.getShopId()) {
                    auditLogList.add(shopAuditLog);
                }
            }
            shop.setAuditLogs(auditLogList);
            shopList.add(shop);
        }
        //方到返回里面
        return new PageList<Shop>(totals, shopList);
    }
}
